package com.mapforge.selection;

import java.awt.Rectangle;
import java.util.Iterator;
import java.util.NoSuchElementException;

public final class RectangularIterator implements Iterator<ComparablePoint> {
	Rectangle rect;
	int x, y, right, bottom;

	public RectangularIterator(Rectangle rect) {
		this.rect = rect;
		x = rect.x;
		y = rect.y;
		right = x + rect.width;
		bottom = y + rect.height;
	}

	public boolean hasNext() {
		return y < bottom;
	}

	@Override
	public ComparablePoint next() {
		if (!hasNext()) {
			throw new NoSuchElementException(
					"Rectangular selection iterator has returned all know points");
		}
		ComparablePoint point = new ComparablePoint(x, y);
		x++;
		if (x >= right) {
			x = rect.x;
			y++;
		}
		return point;
	}

	public void remove() {
		throw new UnsupportedOperationException(
				"Cannot remove points from rectangular selections");
	}
}
